home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / gnu / gnulib / sipp / libsipp / granite.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-05-03  |  2.3 KB  |  90 lines

  1. /**
  2.  ** sipp - SImple Polygon Processor
  3.  **
  4.  **  A general 3d graphic package
  5.  **
  6.  **  Copyright Equivalent Software HB  1992
  7.  **
  8.  ** This program is free software; you can redistribute it and/or modify
  9.  ** it under the terms of the GNU General Public License as published by
  10.  ** the Free Software Foundation; either version 1, or any later version.
  11.  ** This program is distributed in the hope that it will be useful,
  12.  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  ** GNU General Public License for more details.
  15.  ** You can receive a copy of the GNU General Public License from the
  16.  ** Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  **/
  18.  
  19. /**
  20.  ** granite.c - Granite shader: using a 1/f fractal noise function for 
  21.  **             mixing color values.  
  22.  **/
  23.  
  24. #include <math.h>
  25. #include <stdio.h>
  26.  
  27. #include <sipp.h>
  28. #include <noise.h>
  29. #include <shaders.h>
  30. #include <geometric.h>
  31.  
  32.  
  33. extern bool noise_ready;
  34.  
  35.  
  36. static void 
  37. granite (p, color, gd)
  38.     Vector *p;
  39.     Color *color;
  40.     Granite_desc *gd;
  41. {
  42.     int i;
  43.     Vector v;
  44.     double temp, n = 0.5, freq = 1.0;
  45.  
  46.     v = *p;
  47.     
  48.     for (i = 0; i < 6 ; freq *= 2.0, i++) {
  49.         v.x *= 4.0 * freq;
  50.         v.y *= 4.0 * freq;
  51.         v.z *= 4.0 * freq;
  52.         temp = 0.5 * noise(&v);
  53. /*        temp = fabs(temp);*/
  54.         n += temp / freq;
  55.     }
  56.  
  57.    color->red = gd->col1.red * n + gd->col2.red * (1.0 - n);
  58.    color->grn = gd->col1.grn * n + gd->col2.grn * (1.0 - n);
  59.    color->blu = gd->col1.blu * n + gd->col2.blu * (1.0 - n);
  60. }
  61.  
  62.  
  63. void
  64. granite_shader(pos, normal, texture, view_vec, lights, gd, color, opacity)
  65.     Vector        *pos;
  66.     Vector        *normal;
  67.     Vector        *texture;
  68.     Vector        *view_vec;
  69.     Lightsource   *lights;
  70.     Granite_desc  *gd;
  71.     Color         *color;
  72.     Color         *opacity;
  73. {
  74.     Vector     tmp;
  75.     Surf_desc  surface;
  76.  
  77.     if (!noise_ready) {
  78.         noise_init();
  79.     }
  80.  
  81.     VecScalMul(tmp, gd->scale, *texture);
  82.     granite(&tmp, &surface.color, gd);
  83.     surface.ambient  = gd->ambient;
  84.     surface.specular = gd->specular;
  85.     surface.c3       = gd->c3;
  86.     surface.opacity  = gd->opacity;
  87.     basic_shader(pos, normal, texture, view_vec, lights, &surface, 
  88.                  color, opacity);
  89. }
  90.